# -*- coding:utf-8 -*-
import json
from bs4 import BeautifulSoup
#from PIL import Image
from StringIO import StringIO
import requests
import cookielib
import sys
import re
#import chardet
import codecs

'''
获取武汉大学梅操电影
'''
req = requests.session()

def movie_info():
    #登录
    url = 'http://vhost.whu.edu.cn/gh/xywh.php?Class_Type=0&Class_ID=42'
    r = req.get(url)
    #return r.content.decode('gb2312').encode('utf-8')
    return r.content

def parse_movie_info(html):
    # print html
    soup = BeautifulSoup(html)
    css_link = soup.select('link[href="lg_Page/Css/Page.css"]')[0]
    table = css_link.find_next_siblings("table")[0]
    # print table
    tr_index = 0
    td_set = table.select('td')
    date = td_set[2]
    url = td_set[1].a.get('href')
    #跳转信息页面
    r = req.get('http://vhost.whu.edu.cn/gh/'+url)
    html = r.content.decode('gb2312').encode('utf-8')
    soup = BeautifulSoup(html)
    p_set = soup.select('div.ConShow p')
    movie_name = p_set[0].get_text().strip()
    movie_time = p_set[1].get_text().strip()
    movie_place = p_set[2].get_text().strip()
    movie = {
        'name':movie_name,
        'time':movie_time,
        'place':movie_place,
    }
    print json.dumps(movie, indent=4, ensure_ascii=False)
    return movie


if __name__ == '__main__':

    html = movie_info()
    parse_movie_info(html)

